package com.hongdee.insurance.car.service.impl;

import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.car.dao.ITCrmCarWarningConfDao;
import com.hongdee.insurance.car.entity.TCrmCarWarningConf;
import com.hongdee.insurance.car.service.ITCrmCarWarningConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/6/7.
 */
@Service
public class TCrmCarWarningConfServiceImpl implements ITCrmCarWarningConfService {

    @Autowired
    private ITCrmCarWarningConfDao carWarningConfDao;

    private final String DEFWARNINGTYPE="bx";


    /**
     * 新增车辆预警信息
     * @param carWarningConf
     * @param uid
     * @throws Exception
     */
    @Override
    public void saveCarWarningConf(TCrmCarWarningConf carWarningConf, String uid) throws Exception {
        carWarningConf.setWarningType(DEFWARNINGTYPE);
        carWarningConf.setCreateTime(new Date().getTime());
        carWarningConf.setCreateUser(uid);
        Map map=this.getCarWarningConf(carWarningConf);
        if(map!=null){
            if(map.size()>0) {
                throw new Exception("预警信息已经存在，请调整生重新录入！！");
            }
        }
        carWarningConfDao.saveOrUpdate(carWarningConf);
    }

    /**
     * 编辑车辆预警信息
     * @param carWarningConf
     * @param uid
     * @throws Exception
     */
    @Override
    public void updCarWarningConf(TCrmCarWarningConf carWarningConf, String uid) throws Exception {
        carWarningConf.setWarningType(DEFWARNINGTYPE);
        carWarningConf.setUpdateTime(new Date().getTime());
        carWarningConf.setUpdateUser(uid);
        Map map=this.getCarWarningConfByUpd(carWarningConf);
        if(map!=null){
            if(map.size()>0) {
                throw new Exception("预警信息已经存在，请调整生重新录入！！");
            }
        }
        carWarningConfDao.saveOrUpdate(carWarningConf);
    }

    private Map getCarWarningConfByUpd(TCrmCarWarningConf carWarningConf) throws Exception {
        String sql="select * from T_CRM_CAR_WARNING_CONF where brand=? and model=? and series=? and configure=? and warning_type=? and id<> ?";
        return carWarningConfDao.info(sql, StringUtil.formatEmpty(carWarningConf.getBrand()),StringUtil.formatEmpty(carWarningConf.getModel()),
                StringUtil.formatEmpty(carWarningConf.getSeries()),StringUtil.formatEmpty(carWarningConf.getConfigure()),
                DEFWARNINGTYPE,carWarningConf.getId());
    }

    /**
     * 获取某一类下是否已经存在相同的预警信息，如存在则不允许新增
     * @param carWarningConf
     * @return
     * @throws Exception
     */
    @Override
    public Map getCarWarningConf(TCrmCarWarningConf carWarningConf) throws Exception {
        String sql="select * from T_CRM_CAR_WARNING_CONF where brand=? and model=? and series=? and configure=? and warning_type=? ";
        return carWarningConfDao.info(sql, StringUtil.formatEmpty(carWarningConf.getBrand()),StringUtil.formatEmpty(carWarningConf.getModel()),
                StringUtil.formatEmpty(carWarningConf.getSeries()),StringUtil.formatEmpty(carWarningConf.getConfigure()),
                DEFWARNINGTYPE);
    }

    /**
     * 分页查询预警信息
     * @param data
     * @param uid
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    @Override
    public Map page(Map data, String uid, String start, String limit) throws Exception {
        StringBuffer sql=new StringBuffer("" +
                "select conf.*,brand.b_name,series.car_series_name from t_crm_car_warning_conf conf " +
                "left JOIN  " +
                "t_bc_brand brand " +
                "ON " +
                "conf.brand=brand.b_code " +
                "left JOIN " +
                "t_bc_car_series series " +
                "ON " +
                "conf.series=series.car_series_code and conf.brand=series.brand_code " +
                "where conf.warning_type=? ");
        List<Object> params=new ArrayList<>();
        params.add(DEFWARNINGTYPE);
        if(!"".equals(StringUtil.formatEmpty(data.get("brand")))){
            sql.append(" and conf.brand=? ");
            params.add(StringUtil.formatEmpty(data.get("brand")));
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("model")))){
            sql.append(" and conf.model=? ");
            params.add(StringUtil.formatEmpty(data.get("model")));
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("series")))){
            sql.append(" and conf.series=? ");
            params.add(StringUtil.formatEmpty(data.get("series")));
        }
        if(!"".equals(StringUtil.formatEmpty(data.get("configure")))){
            sql.append(" and conf.configure=? ");
            params.add(StringUtil.formatEmpty(data.get("configure")));
        }
        return carWarningConfDao.page(sql.toString(),params.toArray(),start,limit);
    }


    /**
     * 查询预警配置信息
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Map info(String id) throws Exception {
        String sql="select conf.*,brand.b_name,series.car_series_name from t_crm_car_warning_conf conf " +
                "left JOIN  " +
                "t_bc_brand brand " +
                "ON " +
                "conf.brand=brand.b_code " +
                "left JOIN " +
                "t_bc_car_series series " +
                "ON " +
                "conf.series=series.car_series_code and conf.brand=series.brand_code where conf.id=?";
        return this.carWarningConfDao.info(sql,id);
    }


    /**
     * 获得预警信息
     * @param brand
     * @param series
     * @param model
     * @param configure
     * @return
     * @throws Exception
     */
    @Override
    public Map getWarningInfo(String brand,String series,String model,String configure) throws Exception {
        StringBuffer sql=new StringBuffer();
        List<String> params=new ArrayList<>();
        sql.append("select * from t_crm_car_warning_conf conf where warning_type='bx' and conf.configure <>''  " +
                "and brand=? and series=? and model=? and configure=? ");
        params.add(brand);
        params.add(series);
        params.add(model);
        params.add(configure);
        sql.append("UNION " +
                "select * from t_crm_car_warning_conf conf where warning_type='bx' " +
                "and brand=? and series=? and model=? ");
        params.add(brand);
        params.add(series);
        params.add(model);
        sql.append("UNION " +
                "select * from t_crm_car_warning_conf conf where warning_type='bx' " +
                "and brand=? and series=? ");
        params.add(brand);
        params.add(series);
        List<Map<String, Object>> list=this.carWarningConfDao.list(sql.toString(),params.toArray());
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }


}
